/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2007，所有权利保留。
 * 
 * 文件名：	RB_v20r05.java
 * 模块说明：	
 * 修改历史：
 * Sep 29, 2007 - zhangyanbo - 创建。
 */
package com.hd123.rumba.upgrade.v20.r07;

import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

import org.apache.log4j.Logger;

import com.hd123.rumba.cartridge.CartRumba;
import com.hd123.rumba.cartridge.FVRumbaQt;
import com.hd123.rumba.cartridge.ICartridgeMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.option.biz.IBOptionMgr;
import com.hd123.rumba.qt.def.IQueryDef;
import com.hd123.rumba.qt.def.IQueryDefFuncView;
import com.hd123.rumba.qt.def.QueryDefFuncView;
import com.hd123.rumba.runtime.HibernateSession;
import com.hd123.rumba.runtime.NamingMgr;
import com.hd123.rumba.sysclock.TaskScheduleBuffer;
import com.hd123.rumba.upgrade.DBCommand;
import com.hd123.rumba.upgrade.Upgrade;
import com.hd123.rumba.util.Version;

/**
 * 2.0.6.0->2.0.7.0
 * 
 * @author zhangyanbo
 * 
 */
public class RB_v20r06 extends Upgrade {

  public RB_v20r06() throws Exception {
    super();
    baseVersion = new Version("2.0.6.0");
    targetVersion = new Version("2.0.7.1168");
    cartUuid = this.getCart(CartRumba.class).getUuid();
    logger = Logger.getLogger(this.getClass());
  }

  @Override
  public void executeDdl() throws BusinessException {
    DBCommand cmd = new DBCommand();
    execFile(cmd, "upgrade.sql");
  }

  @Override
  public void executeData() throws BusinessException {
    // 升级qt
    logger.info("升级查询工具的权限...");
    try {
      upgradeQt();
    } catch (HibernateException e) {
      logger.error(e);
      throw new BusinessException(e);
    }

    registerOptions();
  }

  private void registerOptions() throws BusinessException {
    logger.info("开始注册选项类信息(OptionalClassInfo)……");
    IBOptionMgr optMgr = (IBOptionMgr) NamingMgr.getInstance().lookupMgr(IBOptionMgr.class);
    try {
      optMgr.registerClassInfoT(TaskScheduleBuffer.class, IBOptionMgr.VALUE_INDIVIDUATION_DOMAIN,
          Boolean.FALSE, Boolean.TRUE);
    } catch (HibernateException e) {
      throw new BusinessException(e);
    }
  }

  private void upgradeQt() throws BusinessException, HibernateException {
    ICartridgeMgr cartMgr = (ICartridgeMgr) NamingMgr.getInstance().lookupMgr(ICartridgeMgr.class);
    Session sess = HibernateSession.getSession();
    Transaction tx = sess.beginTransaction();
    try {
      List list = sess.find("from " + IQueryDef.class.getName()
          + " o where o.permission is not null");
      for (int i = 0; i < list.size(); i++) {
        IQueryDef queryDef = (IQueryDef) list.get(i);
        queryDef.setPermission(null);
        boolean exists = false;
        for (int j = 0; j < queryDef.getFucvViews().size(); j++) {
          if (queryDef.getFucvViews().get(j).getFuncView().getUuid().equals(
              this.getCFV(FVRumbaQt.class).getUuid())) {
            exists = true;
            break;
          }
        }
        if (!exists) {
          IQueryDefFuncView qdfv = new QueryDefFuncView();
          qdfv.setQueryDef(queryDef);
          qdfv.setFuncView(cartMgr.getFunctionView(this.getCFV(FVRumbaQt.class).getUuid()));
          queryDef.getFucvViews().add(qdfv);
          sess.saveOrUpdate(queryDef);
        }
      }
      tx.commit();
    } catch (Exception e) {
      tx.rollback();
      throw new BusinessException(e);
    }
  }
}
